package com.eascs.workflow.wf.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.workflow.common.dao.BaseDao;
import com.eascs.workflow.common.entity.wf.DicData;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;

@Repository
public class ProcessTemplateDao extends BaseDao {
	private static final String TABLE_NAME = "wf_template";

	public ProcessTemplate getPocessTemplate(String systemModule, String type, String version) {
		String sql = "select * from " + TABLE_NAME
				+ " where system_module = ? and process_type = ? and process_version = ? and publish_status = ?";
		String publish_status = ProcessTemplate.PUBLISH_STATUS_YES;
		return workflowJdbcSession.findEntity(sql, ProcessTemplate.class, systemModule, type, version, publish_status);
	}

	public ProcessTemplate getPocessTemplate(String systemModule, String type) {
		String sql = "select * from " + TABLE_NAME
				+ " where system_module = ? and process_type = ? and publish_status = ?";
		String publish_status = ProcessTemplate.PUBLISH_STATUS_YES;
		return workflowJdbcSession.findEntity(sql, ProcessTemplate.class, systemModule, type, publish_status);
	}

	public List<ProcessTemplate> getPocessTemplateList(String systemModule) {
		String sql = "select * from " + TABLE_NAME + " where system_module = ? and publish_status = ?";
		String publish_status = ProcessTemplate.PUBLISH_STATUS_YES;
		return workflowJdbcSession.findEntitys(sql, ProcessTemplate.class, systemModule, publish_status);
	}

	public PageResult<Object[]> getPocessTemplateList(Map<String, String> param, int pageSize, int pageIndex) {
		String sql = "select process_no,process_name,catagory,process_type,process_des,system_module,publish_status from "
				+ TABLE_NAME + " where is_del = 0";
		String processName = (String) param.get("processName");
		if (StringUtils.isNotEmpty(processName)) {
			sql = sql + " and process_name like '%" + processName + "%'";
		}
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, pageSize, pageIndex, sql.toString());
		return super.findDataWithPage(sqlPageQuery);
	}

	/**
	 * 修改流程膜模板状态 param:isEnable Y启用 N禁用
	 */
	public int updateTemplateStatus(String processNo, String isEnable) {
		if (isEnable.equals(ProcessTemplate.PUBLISH_STATUS_YES)) {
			String querySql = "select process_no,process_name,process_type,process_des,system_module,publish_status,process_version from wf_template where process_no = ?";
			ProcessTemplate processTemplate = workflowJdbcSession.findEntity(querySql, ProcessTemplate.class,
					new Object[] { processNo });
			String updateSql = "update wf_template set publish_status = ? where process_type = ? and system_module = ?";
			workflowJdbcSession.execute(updateSql, new Object[] { ProcessTemplate.PUBLISH_STATUS_NO,
					processTemplate.getProcessType(), processTemplate.getSystemModule() });
		}
		String updateSql = "update wf_template set publish_status = ? where process_no = ?";
		return workflowJdbcSession.execute(updateSql, new Object[] { isEnable, processNo });
	}

	/**
	 * 修改流程膜模板时效
	 */
	public int updateTimeEffective(String processNo, int timeEffective) {
		String sql = "update wf_template set time_effective=? where process_no=? ";
		return workflowJdbcSession.execute(sql, new Object[] { timeEffective, processNo });
	}

	/**
	 * 删除模板
	 * 
	 * @param processNo
	 * @return
	 */
	public int delProcessTemplate(String processNo) {
		String sql = "update wf_template set is_del = 1 where process_no=? ";
		return workflowJdbcSession.execute(sql, new Object[] { processNo });
	}

	public List<DicData> getProcessTypeByCatagory(String catagory) {
		String sql = "SELECT dd.`code`,dd.`name` from wf_dictionary_type dt LEFT JOIN wf_dictionary_data dd on dt.id = dd.type ";
		sql += " where dt.is_lock ='N' and dd.is_lock ='N' and dt.type ='processType' ";
		sql += " and dd.code in (SELECT DISTINCT t.process_type from wf_template t where t.catagory= ?)";
		return workflowJdbcSession.findEntitys(sql, DicData.class, catagory);
	}
}
